var $j = jQuery.noConflict(); 
  //var $ = jQuery; 
$j(function () {
	var bar = $j('.bar');
	var percent = $j('.percent');
	var showimg = $j('#showimg');
	var progress = $j(".progress");
	var files = $j(".files");
	var btn = $j(".btn span");
	//$j("#fileupload").wrap("<form id='myupload' action='http://files.bwphp.com/teacher.php' method='post' enctype='multipart/form-data'></form>");
    $j("#fileupload").wrap("<form id='myupload' action='http://files.bwphp.com/file/image/upload/userthumb' method='post' enctype='multipart/form-data'></form>");
	$j("#fileupload").change(function(){
		$j("#myupload").ajaxSubmit({
			dataType:  'json',
			beforeSend: function() {
        		showimg.empty();
				progress.show();
        		var percentVal = '0%';
        		bar.width(percentVal);
        		percent.html(percentVal);
				btn.html("上传中...");
    		},
    		uploadProgress: function(event, position, total, percentComplete) {
        		var percentVal = percentComplete + '%';
        		bar.width(percentVal);
        		percent.html(percentVal);
    		},
			success: function(info) {
				if (info.code == 200)
				{
					files.html("<b>"+info.data.name+"("+info.data.size+"k)</b> ");
					//var img = "http://files.bwphp.com/"+info.data.path;
					var img = "http://files.bwphp.com/file/image/show/120/120?path="+info.data.path;
					showimg.html("<img src='"+img+"' width='150' height='80'> <span class='delimg' rel='"+info.data.path+"'>删除</span>");
					btn.html("修改头像");
					$j('#teacherinfo-teacher_thumb').val(info.data.path);
				} 
				else if (info.code == 400)
				{
					btn.html("上传失败");
					bar.width('0')
					files.html(info.msg);
				} 
					
			},
			error:function(xhr){
				btn.html("上传失败");
				bar.width('0')
				files.html(xhr.responseText);
			}
		});
	});
	
	$j(".delimg").live('click',function(){
		var pic = $(this).attr("rel");
		//$.post("http://files.bwphp.com/teacher.php?act=delimg",{imagename:pic},function(msg){
		$.post("http://files.bwphp.com/file/image/remove",{filePath:pic},function(msg){
			if(msg.data==1){
				files.html("删除成功.");
				showimg.empty();
				progress.hide();
				$j('#image_name').val('');
			}else{
				alert(msg);
			}
		});
	}); 
});